package com.atguigu.gmall.product.rpc;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.biz.CategoryBizService;
import com.atguigu.gmall.product.entity.CategoryView;
import com.atguigu.gmall.product.entity.SkuImage;
import com.atguigu.gmall.product.entity.SkuInfo;
import com.atguigu.gmall.product.entity.SpuSaleAttr;
import com.atguigu.gmall.product.service.SkuImageService;
import com.atguigu.gmall.product.service.SkuInfoService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.atguigu.gmall.web.SkuDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author:ahang查询商品详情远程调用接口
 * @create:2022-10-04 19:12
 * @Description:
 */
@RestController
@RequestMapping("/api/inner/product")
public class SkudetailRpcController {

    @Autowired
    SkuInfoService skuInfoService;

    @Autowired
    SkuImageService skuImageService;

    @Autowired
    CategoryBizService categoryBizService;

    @Autowired
    SpuSaleAttrService spuSaleAttrService;

    /**
     * 查询skuinfo数据
     * @return
     */
    @GetMapping("/skuInfo/{skuId}")
    public Result<SkuInfo> getSkuInfo(@PathVariable("skuId") Long skuId){
        SkuInfo skuInfo = skuInfoService.getById(skuId);
        return Result.ok(skuInfo);
    }

    /**
     * 查询skuImageList数据
     * @param skuId
     * @return
     */
    @GetMapping("/skuImageList/{skuId}")
    public Result<List<SkuImage>> getImage(@PathVariable("skuId") Long skuId){
        List<SkuImage> skuImageList = skuImageService.getSkuImage(skuId);
        return Result.ok(skuImageList);
    }

    /**
     * 查询三级分类值和名
     * @param skuId
     * @return
     */
    @GetMapping("/category/{skuId}")
    public Result<SkuDetailVo.CategoryView> getCategory(@PathVariable("skuId") Long skuId){
        //自己封装的categoryView
        CategoryView categoryView = categoryBizService.getCategory(skuId);

        SkuDetailVo.CategoryView newCategoryView = new SkuDetailVo.CategoryView();
        newCategoryView.setCategory1Id(categoryView.getC1id());
        newCategoryView.setCategory1Name(categoryView.getC1name());
        newCategoryView.setCategory2Id(categoryView.getC2id());
        newCategoryView.setCategory2Name(categoryView.getC2name());
        newCategoryView.setCategory3Id(categoryView.getC3id());
        newCategoryView.setCategory3Name(categoryView.getC3name());

        return Result.ok(newCategoryView);
    }

    /**
     * 查询实时价格
     * 价格查询比较频繁,不应该查整张表获取该字段
     * @return
     */
    @GetMapping("/getPrice/{skuId}")
    public Result<BigDecimal> getPrice(@PathVariable("skuId") Long skuId){
        BigDecimal price = skuInfoService.getPrice(skuId);
        return Result.ok(price);
    }

    /**
     * 根据skuid查询销售属性的名和值并且标记
     * @param spuId
     * @return
     */
    @GetMapping("/getSpuSaleAttrAndValue/{spuId}/{skuId}")
    public Result<List<SpuSaleAttr>> getSpuSaleAttrListAndMark(@PathVariable("spuId") Long spuId,
                                                            @PathVariable("skuId") Long skuId){
        List<SpuSaleAttr> spuSaleAttrList = spuSaleAttrService.getSpuSaleAttrListAndMark(spuId,skuId);
        return Result.ok(spuSaleAttrList);
    }

    /**
     * 根据spuid查询出这个spu下面所有的sku涉及到的所有属性组合
     * @param spuId
     * @return
     */
    @GetMapping("/valueskujson/{spuId}")
    public Result<String> getSpuValueSkuJson(@PathVariable("spuId") Long spuId){
        String json = spuSaleAttrService.getSpuValueSkuJson(spuId);
        return Result.ok(json);
    }

    /**
     * 获取所有的skuid
     * @return
     */
    @GetMapping("/allSkuId")
    public Result<List<Long>> getAllSkuId(){
        List<Long> skuIds = skuInfoService.getAllSkuId();
        return Result.ok(skuIds);
    }
}
